package com.yimu.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yimu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yimu.system.domain.Devices;
import com.yimu.system.mapper.DeviceCompressormodeInfoMapper;
import com.yimu.system.domain.DeviceCompressormodeInfo;
import com.yimu.system.service.IDeviceCompressormodeInfoService;
import com.yimu.common.core.text.Convert;

/**
 * 设备记录Service业务层处理
 * 
 * @author Christine
 * @date 2020-08-26
 */
@Service
public class DeviceCompressormodeInfoServiceImpl implements IDeviceCompressormodeInfoService 
{
    @Autowired
    private DeviceCompressormodeInfoMapper deviceCompressormodeInfoMapper;

    /**
     * 查询设备记录
     * 
     * @param Id 设备记录ID
     * @return 设备记录
     */
    @Override
    public DeviceCompressormodeInfo selectDeviceCompressormodeInfoById(Long Id)
    {
        return deviceCompressormodeInfoMapper.selectDeviceCompressormodeInfoById(Id);
    }

    /**
     * 查询设备记录列表
     * 
     * @param deviceCompressormodeInfo 设备记录
     * @return 设备记录
     */
    @Override
    public List<DeviceCompressormodeInfo> selectDeviceCompressormodeInfoList(DeviceCompressormodeInfo deviceCompressormodeInfo)
    {
        return deviceCompressormodeInfoMapper.selectDeviceCompressormodeInfoList(deviceCompressormodeInfo);
    }

    /**
     * 新增设备记录
     * 
     * @param deviceCompressormodeInfo 设备记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDeviceCompressormodeInfo(DeviceCompressormodeInfo deviceCompressormodeInfo)
    {
        int rows = deviceCompressormodeInfoMapper.insertDeviceCompressormodeInfo(deviceCompressormodeInfo);
        insertDevices(deviceCompressormodeInfo);
        return rows;
    }

    /**
     * 修改设备记录
     * 
     * @param deviceCompressormodeInfo 设备记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDeviceCompressormodeInfo(DeviceCompressormodeInfo deviceCompressormodeInfo)
    {
        deviceCompressormodeInfoMapper.deleteDevicesByDeviceNum(deviceCompressormodeInfo.getId());
        insertDevices(deviceCompressormodeInfo);
        return deviceCompressormodeInfoMapper.updateDeviceCompressormodeInfo(deviceCompressormodeInfo);
    }

    /**
     * 删除设备记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDeviceCompressormodeInfoByIds(String ids)
    {
        deviceCompressormodeInfoMapper.deleteDevicesByDeviceNums(Convert.toStrArray(ids));
        return deviceCompressormodeInfoMapper.deleteDeviceCompressormodeInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除设备记录信息
     * 
     * @param Id 设备记录ID
     * @return 结果
     */
    @Override
    public int deleteDeviceCompressormodeInfoById(Long Id)
    {
        deviceCompressormodeInfoMapper.deleteDevicesByDeviceNum(Id);
        return deviceCompressormodeInfoMapper.deleteDeviceCompressormodeInfoById(Id);
    }

    /**
     * 新增终端列表信息
     * 
     * @param deviceCompressormodeInfo 设备记录对象
     */
    public void insertDevices(DeviceCompressormodeInfo deviceCompressormodeInfo)
    {
        List<Devices> devicesList = deviceCompressormodeInfo.getDevicesList();
        Long Id = deviceCompressormodeInfo.getId();
        if (StringUtils.isNotNull(devicesList))
        {
            List<Devices> list = new ArrayList<Devices>();
            for (Devices devices : devicesList)
            {
                devices.setId(Id);
                list.add(devices);
            }
            if (list.size() > 0)
            {
                deviceCompressormodeInfoMapper.batchDevices(list);
            }
        }
    }
}
